package com.asiainfo_linkage.mvc.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.nutz.ioc.annotation.InjectName;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import com.asiainfo_linkage.model.AddGroup;
import com.asiainfo_linkage.model.AddMember;
import com.asiainfo_linkage.model.AuthorizationNode;
import com.asiainfo_linkage.model.RemPlan;
import com.asiainfo_linkage.model.RemTask;
import com.asiainfo_linkage.model.RemTaskMember;
import com.asiainfo_linkage.service.CycleService;
import com.asiainfo_linkage.service.GroupQueryService;
import com.asiainfo_linkage.service.MemberQueryService;

@IocBean
@InjectName
public class CycleDayAction{
	
	@Inject
	private CycleService cycleService;
	
	public void setCycleService(CycleService cycleService) {
		this.cycleService = cycleService;
	}
	@Inject
	private GroupQueryService groupQueryService;
		
	public void setGroupQueryService(GroupQueryService groupQueryService) {
			this.groupQueryService = groupQueryService;
	}
	
	@Inject
	private MemberQueryService memberQueryService;
		
	public void setMemberQueryService(MemberQueryService memberQueryService) {
			this.memberQueryService = memberQueryService;
	}
	
	/**
	 * 进入添加数据页面。
	 */
	@At("/add")
	@Ok(value = "jsp:cycleDay.cycleday")
	public Map<String, Object> add() {
			Map<String, Object> map = new HashMap<String, Object>();
			List<AddGroup> list = groupQueryService.query(null, null);
			List<AddMember> listmember = memberQueryService.query(null, null);
			map.put("listmember", listmember);
			map.put("list", list);
			return map;
			
	}
	
	/**
	 * 保存添加数据页面,保存完毕后跳转到列表页面。
	 */
	@At("/save")
	@Ok(value = "jsp:reminder.single")
	
	public void save(@Param("::remPlan.")RemPlan remPlan, @Param("::remTask.")RemTask remTask,String taskMemberStr) {
		remTask.setRemTaskMembers(this.strToList(taskMemberStr));//存储数据。
		this.cycleService.add(remPlan, remTask); 	
	}
	
	/**
	 * 将字符串序列转化为对象列表。
	 * @param taskMemberStr
	 * @return
	 */
	private List<RemTaskMember> strToList(String taskMemberStr){
		List<RemTaskMember> list = new ArrayList<RemTaskMember>();
		if(taskMemberStr!=null && taskMemberStr.length()>0){
			String[] a = taskMemberStr.split(",");
			for(String s:a){
				RemTaskMember remTaskMember = new RemTaskMember();
				try{
					Long member_id = Long.parseLong(s);
					remTaskMember.setMember_id(member_id);
				}catch (Exception e) {}
				list.add(remTaskMember);
			}
		}
		
		return list;
	}
	
	@At("remTask/findMember2")
	@Ok(value = "json")//处理成功后结果转向页面
	public List<AuthorizationNode> findMember(long groupId) {
		List<AddGroup> groups = groupQueryService.query(null, null);
		List<AddMember> members = memberQueryService.query(null, null);
		return toAuthorizationNode(groups,members);
	}
	private List<AuthorizationNode> toAuthorizationNode(List<AddGroup> groups,List<AddMember> members){
		List<AuthorizationNode> list = new ArrayList<AuthorizationNode>();
		for (AddGroup group:groups){
			AuthorizationNode node = new AuthorizationNode();	
			list.add(node);
			node.setName(group.getGROUP_NAME());
			node.setOpen(true);
			node.setValue(String.valueOf(group.getGROUP_ID()));
			List<AuthorizationNode> children = new ArrayList<AuthorizationNode>();
			node.setChildren(children);
			for(AddMember member:members){
				if(member.getGROUP_ID().equals(group.getGROUP_ID())){
					AuthorizationNode childnode = new AuthorizationNode();
					childnode.setName(member.getMEMBER_NAME());
					childnode.setValue(String.valueOf(member.getMEMBER_ID()));
					children.add(childnode);
				}
			}
		}
		return list;
	}
}
